<?php
namespace common\models;

use Yii;
use yii\helpers\Json;
use yii\httpclient\Client;
use api\components\ApiModel;
use api\components\OperateException;
use api\components\TransmitException;

/**
 * 认证模型
 * @author ZCY
 */
class Certificate extends ApiModel
{
    const SCENARIO_GETTOKEN = 'get_token';
    const SCENARIO_CERTIFICATE = 'certificate';
    
    
 
    const IDTYPE_IDENTITYCARD=1;      //身份证
    const IDTYPE_DRIVINGLICENCE=2;    //驾驶证
    const IDTYPE_RESIDENCE=3;         //户口本
 

    public $token;

    
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'token'          => '授权token',
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            self::SCENARIO_GETTOKEN     => [],

        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['token'], 'required'],      
        ];
    }

 
    /**
     * 获取认证服务器授权token
     */
    public function getToken()
    {
        $token = Yii::$app->cache->get('token');
        if (!$token) {
            $time = Yii::$app->formatter->asTimestamp('now');               //时间戳     
            $appkey = Yii::$app->params['certificate_info']['appkey'];           
            $appsecret = Yii::$app->params['certificate_info']['appsecret'];
            $params = [];
            ksort($params);
            $string = http_build_query($params);
            $result = md5($appkey . $string . $appsecret . $time);
            $sign = strtoupper($result);
            $appIp=Yii::$app->request->userIP;    //操作中IP
              
            $url=Yii::$app->params['certificate_info']['url'].Yii::$app->params['certificate_info']['method']['getToken'];
            
            $headers=[
                'FZM-Ca-Timestamp' => $time,
                'FZM-Ca-AppKey'    => $appkey,
                'FZM-Ca-AppIp'     => Yii::$app->request->userIP,
                'FZM-Ca-Signature' => $sign,           
            ];

            $result=$this->curl($url, $params,$headers);
            
            if (isset($result['code']) && $result['code'] === 200) {
                $token = $result['data']['token'];
                $token_expire = $result['data']['deadline'] - Yii::$app->formatter->asTimestamp('now') - 30;
                Yii::$app->cache->set('token', $token, $token_expire);
            } else {
                throw new TransmitException($result['message']);
            }
        }
        return $token;
    }
    
    
    /**
     * 两照比对
     * @param unknown $mid
     * @param unknown $matchmid
     */
    public static function piccompare($mid,$matchmid){
        $time = Yii::$app->formatter->asTimestamp('now');               //时间戳
        $appkey = Yii::$app->params['certificate_info']['appkey'];
        $appsecret = Yii::$app->params['certificate_info']['appsecret'];
        $params = [
            'mid' => $mid,
            'matchmid' => $matchmid,
        ];
        ksort($params);
        $string = http_build_query($params);
        $result = md5($appkey . $string . $appsecret . $time);
        $sign = strtoupper($result);
        $appIp=Yii::$app->request->userIP;    //操作中IP
        
        $url=Yii::$app->params['certificate_info']['url'].Yii::$app->params['certificate_info']['method']['picCompare'];
        $headers=[
            'FZM-Ca-Timestamp' => $time,
            'FZM-Ca-AppKey'    => $appkey,
            'FZM-Ca-AppIp'     => Yii::$app->request->userIP,
            'FZM-Ca-Signature' => $sign,
        ];

        $result=(new self())->curl($url, $params,$headers,'post',40);
        if (isset($result['code']) && $result['code'] === 200) {
            return true;
        } else {
            throw new TransmitException($result['message']);
        }
    }
    
    
    
    /**
     * 上传认证
     */
   public function certificate(){
       if (!$this->validate()) {
           self::getErrorMessge($this);
       }
       $appkey = Yii::$app->params['message_info']['appkey']; 
       $params = [
           "idtype"           => $this->idtype,
           'token'            => $this->token,
           'myfile'           => $this->myfile,
           'extend_param'     => urlencode(trim($this->extend_param)),
       ];
       
       $url=Yii::$app->params['certificate_info']['url'].Yii::$app->params['certificate_info']['method']['certificate'];       
       $headers=[
           'FZM-Ca-AppKey'    => $appkey,
       ];
       $result=$this->curl($url, $params,$headers);
       return $result;
   }
    
}
